
package cn.chochy.my.util;

/**
 * 地址转换
 *
 * @author MLeo
 */
public class UrlCreate {

    /**
     * ID编码
     *
     * @param id
     * @return int
     */
    public static int idCoding(int id) {
        id = ((id + 2) * 153 - 3);
        return id;
    }

    /**
     * ID解码
     *
     * @param o_id
     * @return int
     */
    public static int idDecoding(int o_id) {
        o_id = (o_id + 3) / 153 - 2;
        return o_id;
    }

    /**
     * 网页地址编码
     *
     * @param url
     * @param id
     * @return String
     */
    public static String urlCoding(String url, int id) {
        url = url + "-" + ((id + 2) * 153 - 3) + ".html";
        return url;
    }

    /**
     * Action地址转换
     *
     * @param url
     * @return String
     */
    public static String actionCreate(String url) {
        int start = url.lastIndexOf("-");
        int end = url.lastIndexOf(".");
        if(start >0){
            url = url.substring(0, start) + ".action?parameter=" + url.substring(start + 1, end);
        }else{
           url = url.substring(0, end) + ".action";
        }
        
        return url;
    }

    /**
     * jsp地址转换
     *
     * @param url
     * @return String
     */
    public static String jspCreate(String url) {
        url = "WEB-INF/page" + url.replace(".html", ".jsp");
        return url;
    }

    /**
     * 跳转地址选择
     * @param url
     * @return 
     */
    public static String jumpUrl(String url) {
        if (url.matches(".+[0-9]+\\.html")) {
            url = actionCreate(url);
        } else {
            url = jspCreate(url);
        }
        return url;
    }

//    public static void main(String[] args) {
//        String url = jumpUrl("/content.html");
//        System.out.println(url);
//
//    }
}
